package com.itfreer.arcgis.edit;

import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.itfreer.arcgis.draw.DrawCommand;
import com.itfreer.arcgis.draw.DrawTool;
import com.itfreer.arcgis.draw.DrawToolManager;
import com.itfreer.arcgis.draw.DrawToolType;
import com.itfreer.arcgis.gps.GPSLocationListener;
import com.itfreer.arcgis.gps.GpsOption;
import com.itfreer.arcgis.utils.GeometryUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * GPS点绘画命令
 */
public class GPSPointDrawCommand implements DrawCommand
{
    private Class newClass=null;
    public GPSPointDrawCommand(Class newClass)
    {
        this.newClass = newClass;
    }
    MapView mapView = null;

    @Override
    public void Execute(Map<String,Object>params)
    {
        mapView  = (MapView) params.get("mapview");
        GpsOption.instance.openGPS(mapView);
        GpsOption.instance.getListeners().add(new GPSLocationListener()
        {
            @Override
            public void locationUpdate(Point point)
            {
                //添加异常捕捉，避免异常重复监听事件
                try
                {
                    if(point == null)
                        return;
                    point = new Point(point.getX(),point.getY(),mapView.getSpatialReference());
                    DrawTool drawTool = DrawToolManager.GetCurrentTool();
                    if(drawTool == null || drawTool.GetGeometry() == null || drawTool.GetGeometry().isEmpty())
                    {
                        if(drawTool ==null)
                            drawTool =(DrawTool) newClass.newInstance();

                        if(drawTool == null)
                            return;
                        List<Point> points = new ArrayList<>();
                        points.add(point);
                        points.add(new Point(point.getX(),point.getY()));
                        if(drawTool.getDrawToolType() == DrawToolType.Polygon)
                            points.add(new Point(point.getX(),point.getY()));
                        Geometry geometry1 = EditToolUtils.getGeometryByDrawTypeAndPoints(drawTool.getDrawToolType(),points,mapView.getSpatialReference());
                        drawTool.Draw(mapView, geometry1);
                    }
                    else
                    {
                        Geometry geometry  = drawTool.GetGeometry();
                        List<Point> points = GeometryUtils.geometryToPoints(geometry);
                        points.add(point);
                        Geometry geometry1 = EditToolUtils.getGeometryByDrawTypeAndPoints(drawTool.getDrawToolType(),points,mapView.getSpatialReference());
                        drawTool.UpdateGeometry(geometry1);
                    }
                }
                catch (Exception e)
                {

                }
                finally
                {
                    if(GpsOption.instance.getListeners().contains(this))
                    {
                        GpsOption.instance.getListeners().remove(this);
                        GpsOption.instance.stopGPS();
                    }
                }
            }
        });
    }
}
